<?php
namespace PublicNo\Controller;
use Common\Controller\CommonController;
/**
 * 公众号模块
 */
class PublicNoController extends CommonController {
	protected $_public_no;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 110;
		$this->_public_no = new \PublicNo\Model\PublicNoModel();
		$this->_friend = new \Common\Model\FriendModel();
		$this->_cust = new \Common\Model\CustModel();
	}
    
	
	/**
	 * 搜索公众号
	 */
	public function searchAction(){
		$searchKey = I('request.searchKey');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_search($searchKey , $pageIndex , $pageSize);
	}
	
	private function _search($searchKey , $pageIndex=0 , $pageSize=0){
		if(empty($searchKey)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		
		$data['searchKey'] = $searchKey;
		$publicNoList = $this->_public_no->searchPublicNoList($data);
		$publicNoList = $publicNoList['data'];
		
		$this->data = $publicNoList;
		$this->outPut();
	}
    
    
	/**
	 * 我关注的公众号列表
	 */
	public function publicNoListAction(){
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_publicNoList($pageIndex , $pageSize);
	}
	
	private function _publicNoList($pageIndex=0 , $pageSize=0){
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$publicNoList = $this->_public_no->getPublicNoList($data);
		$publicNoList = $publicNoList['data'];
		if(!empty($publicNoList)){
    		$this->data = $publicNoList;
    		$this->outPut();
    	}else{
    		$this->status = 1904;
    		$this->msg = L('STATUS_1904');
    		$this->outPut();
    	}
	}
	
	
	/**
	 * 修改公众号信息
	 */
	public function updateDetailAction(){
		$publicnoid = (int)I('request.publicnoid');
		$fundesc = I('request.fundesc');
		$accmbody = I('request.accmbody');
		$cstel = I('request.cstel');
		//$menus=array();
		//$menus['menu'] = I('request.menu');
		//$menu = json_encode($menus);
		$menu = I('request.menu');
		$nickname = I('request.nickname');
		$this->_updateDetail($publicnoid,$nickname,$accmbody,$fundesc,$cstel,$menu);
	}
	
	private function _updateDetail($publicnoid,$nickname,$accmbody,$fundesc="",$cstel="",$menu=""){
		if(empty($publicnoid) || empty($nickname) || empty($accmbody)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if(empty($publicNoInfo)){
			$this->status = 1913;
			$this->msg = L('STATUS_1913');
			$this->outPut();
		}
	
		$this->_public_no->startTrans();
		 
		$arr1 = array();
		$arr1['custid'] = $publicNoInfo['custid'];
		$arr1['nickname'] = $nickname;
		$arr1['pinyinname'] = \pinyin::toPingyin($nickname);
		$ret1 = $this->_cust->updateCust($arr1);
		 
		$arr2 = array();
		$arr2['publicnoid'] = $publicnoid;
		$arr2['accmbody'] = $accmbody;
		$arr2['fundesc'] = $fundesc;
		$arr2['cstel'] = $cstel;
		$arr2['menu'] = $menu;
		$ret2 = $this->_public_no->updatePublicNo($arr2);
		 
		if($ret1 && $ret2){
			$this->_public_no->commit();
			
			$this->_setCustInfoCache($arr1);
		}else{
			$this->_public_no->rollback();
			$this->status = 1906;
			$this->msg = L('STATUS_1906');
		}
		 
		$this->outPut();
	}
	
	
	
	/**
	 * 重置密码
	 */
	public function resetPublicNoPwdAction(){
		$publicnoid = I("request.publicnoid");
		$newpassword = I("request.newpassword");
		$newpassword2 = I("request.newpassword2");
		$this->_resetPublicNoPwd($publicnoid , $newpassword,$newpassword2);
	}
	
	private function _resetPublicNoPwd($publicnoid , $newpassword,$newpassword2){
		if(empty($publicnoid) || empty($newpassword) || empty($newpassword2) || $newpassword != $newpassword2){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if($publicNoInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
	
		$custInfo = $this->_cust->getCustInfoById($this->_custInfo['custid']);
		if(empty($custInfo)){
			$this->status = 1913;
			$this->msg = L('STATUS_1913');
			$this->outPut();
		}
	
		$arr = array();
		$arr['custid'] = $custInfo['custid'];
		$arr['password'] = \tools::encodePwd($newpassword);
		$ret = $this->_cust->updateCust($arr);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1914;
			$this->msg = L('STATUS_1914');
			$this->outPut();
		}
	}
	
	
	/**
	 * 公众号详情
	 */
	public function publicNoDetailAction(){
		$publicnoid = (int)I('request.publicnoid');
		$this->_publicNoDetail($publicnoid);
	}
	
	private function _publicNoDetail($publicnoid){
		if(empty($publicnoid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$publicNoInfo = $this->_getPublicNoInfo($publicnoid);
		if(!empty($publicNoInfo)){
			$friendInfo = $this->_friend->getFriend(array('custid'=>$this->_custInfo['custid'] , 'tcustid'=>$publicNoInfo['custid']));
			if(!empty($friendInfo)){
				$publicNoInfo['msgsetting'] = json_decode($friendInfo['msgsetting'] , true);
				$publicNoInfo['isfollow'] = "1";
			}else{
				$publicNoInfo['msgsetting'] = array('push'=>"0");
				$publicNoInfo['isfollow'] = "0";
			}
			
			$publicNoInfo['menu'] = json_decode($publicNoInfo['menu'] , true);
		}
		
		$this->data = $publicNoInfo;
		$this->outPut();
	}
	
	
	/**
	 * 个人公众号详情
	 */
	public function publicNoDetailByCustAction(){
		$custid = (int)I('request.custid');
		$this->_publicNoDetailByCust($custid);
	}
	
	private function _publicNoDetailByCust($custid){
		if(empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$publicNoInfo = $this->_getPublicNoInfoByCust($custid);
		if(!empty($publicNoInfo)){
			$friendInfo = $this->_friend->getFriend(array('custid'=>$this->_custInfo['custid'] , 'tcustid'=>$publicNoInfo['custid']));
			if(!empty($friendInfo)){
				$publicNoInfo['msgsetting'] = json_decode($friendInfo['msgsetting'] , true);
				$publicNoInfo['isfollow'] = "1";
			}else{
				$publicNoInfo['msgsetting'] = array('push'=>"0");
				$publicNoInfo['isfollow'] = "0";
			}
			
			$publicNoInfo['menu'] = json_decode($publicNoInfo['menu'] , true);
		}
	
		$this->data = $publicNoInfo;
		$this->outPut();
	}
	
	
	/**
	 * 公众号自定义菜单
	 */
	public function publicNoMenuAction(){
		$custid = (int)I('request.custid');
		$this->_publicNoMenu($custid);
	}
	
	private function _publicNoMenu($custid){
		if(empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$publicNoBaseInfo = $this->_public_no->getPublicNoBaseInfoByCust($custid);
		if(!empty($publicNoBaseInfo))
			$this->data = json_decode($publicNoBaseInfo['menu'] , true);
		$this->outPut();
	}
	
	
	/**
	 * 关注公众号
	 */
	public function followPublicNoAction(){
		$publicnoid = (int)I('request.publicnoid');
		$this->_followPublicNo($publicnoid);
	}
	
	private function _followPublicNo($publicnoid){
		if(empty($publicnoid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$publicNoInfo = $this->_getPublicNoInfo($publicnoid);
		
		$this->_friend->startTrans();
		
		$data = array();
		$data['custid'] = $publicNoInfo['custid'];
		$data['tcustid'] = $this->_custInfo['custid'];
		$data['status'] = 1;
		$ret1 = $this->_friend->addFriend($data);//对方将自己加为好友
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['tcustid'] = $publicNoInfo['custid'];
		$data['status'] = 1;
		$ret2 = $this->_friend->addFriend($data);//将对方加为自己的好友
		
		if($ret1 && $ret2){
			$this->_friend->commit();
			$this->outPut();
		}else{
			$this->_friend->rollback();
			$this->status = 1901;
			$this->msg = L('STATUS_1901');
			$this->outPut();
		}
	}
	
	
	/**
	 * 取消关注公众号
	 */
	public function cancelFollowFublicNoAction(){
		$publicnoid = (int)I('request.publicnoid');
		$this->_cancelFollowPublicNo($publicnoid);
	}
	
	private function _cancelFollowPublicNo($publicnoid){
		if(empty($publicnoid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$publicNoInfo = $this->_getPublicNoInfo($publicnoid);
		
		$this->_friend->startTrans();
		
		$data = array();
		$data['custid'] = $publicNoInfo['custid'];
		$data['tcustid'] = $this->_custInfo['custid'];
		$ret1 = $this->_friend->deleteFriend($data);
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['tcustid'] = $publicNoInfo['custid'];
		$ret2 = $this->_friend->deleteFriend($data);
		
		if($ret1 && $ret2){
			$this->_friend->commit();
		
			$this->outPut();
		}else{
			$this->_friend->rollback();
			$this->status = 1902;
			$this->msg = L('STATUS_1902');
			$this->outPut();
		}
	}
    
	
	private function _getPublicNoInfo($publicnoid){
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if(empty($publicNoInfo)){
			$this->status = 1903;
			$this->msg = L('STATUS_1903');
			$this->outPut();
		}
		
		return $publicNoInfo;
	}
	
	
	private function _getPublicNoInfoByCust($custid){
		$publicNoInfo = $this->_public_no->getPublicNoInfoByCust($custid);
		if(empty($publicNoInfo)){
			$this->status = 1903;
			$this->msg = L('STATUS_1903');
			$this->outPut();
		}
		
		return $publicNoInfo;
	}
	
	
	/**
	 * 消息设置
	 */
	public function msgSettingAction(){
		$publicnoid = (int)I('request.publicnoid');
		$push = (int)I('request.push');
		$this->_msgSetting($publicnoid , $push);
	}
	
	
	private function _msgSetting($publicnoid , $push){
		if(empty($publicnoid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$publicNoInfo = $this->_getPublicNoInfo($publicnoid);
		 
		$data = array();
		$data['tcustid'] = $publicNoInfo['custid'];
		$data['custid'] = $this->_custInfo['custid'];
		$setting = array('push'=>(string)$push);
		$data['msgsetting'] = json_encode($setting);
		$ret = $this->_friend->updateFriend($data);
		if($ret){
			if($push <= 0)
				$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$publicNoInfo['custid'], $this->_custInfo['custid']);
			else
				$this->_mycache->srem(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$publicNoInfo['custid'], $this->_custInfo['custid']);
	
			$this->outPut();
		}else{
			$this->status = 1905;
			$this->msg = L('STATUS_1905');
			$this->outPut();
		}
	}
	
	
	/**
	 * 获取公众号二维码
	 */
	public function getQRCodeAction(){
		$publicnoid = (int)I('request.publicnoid');
		$this->_getQRCode($publicnoid);
	}
	
	private function _getQRCode($publicnoid){
		$publicNoInfo = $this->_getPublicNoInfo($publicnoid);
		
		$data['publicnoid'] = $publicNoInfo['publicnoid'];
		$data['custid'] = $publicNoInfo['custid'];
		$data['fundesc'] = $publicNoInfo['fundesc'];
		$data['accmbody'] = $publicNoInfo['accmbody'];
    	$data['controller'] = CONTROLLER_NAME;
    	//$data = CONTROLLER_NAME . "://" . json_encode($data);
    	$data = C('QR_CODE_INFO_URL') . "?data=" . \aes::encrypt(json_encode($data) , C('TOKEN_SALT'));
		 
		$qrcodefile = "/tmp/publicNo_{$publicNoInfo['publicnoid']}.png";
		$logofile = "";//准备好的公众号avatar图片地址或图片url
		 
		\tools::makeQrcode($data , $qrcodefile , $logofile);
	}
	
	 
}
